<?php

class Banner extends CI_Controller{
    public function  __construct() {
        parent::__construct();
        $this->load->helper("url");
        $this->load->library("admin_layout"); // Sử dụng thư viện layout
        $this->admin_layout->setLayout("layout/admin"); // load file layout chính (view/layout/frontend.php)
        $this->load->Model("MBanners");
        $this->load->library('pagination'); 
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->helper("text");  
	
    }

    // Controller mặc định cho trang chủ
    public function index(){
        $data['msg']='';
        $data['title'] = "Quản lý Banner";

        $data['navigation']['title'] = 'Quản lý Banner';
		$data['navigation']['active']= 'Danh sách Banner';
        $data['data'] = $this->MBanners->listall();
        $this->admin_layout->view("admin/listBanner",$data);
    }
	
	public function listBanner(){
        $data['msg']='';
        $data['title'] = "Quản lý Banner";

        $data['navigation']['title'] = 'Quản lý Banner';
		$data['navigation']['active']= 'Danh sách Banner';
        $data['data'] = $this->MBanners->listall();
        $this->admin_layout->view("admin/listBanner",$data);
    }
	public function addBanner(){
        $data['msg']='';
        $data['title'] = "Quản lý Banner";
	   
        $data['navigation']['title'] = 'Quản lý Banner';
		$data['navigation']['active']= 'Thêm Banner';


        $config = array(
            array(
                 'field' => 'txtNameImage', 
                 'label' => 'Tên Ảnh', 
                 'rules' => 'required'
            ),
            array(
                 'field' => 'txtMota', 
                 'label' => 'Mô tả', 
                 'rules' => 'required'
            )
        );
        
        $this->form_validation->set_rules($config);
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $this->admin_layout->view("admin/addBanner",$data);
        } else {

            //$image_path = realpath(APPPATH . 'home/trantam/public_html/public\frontend\images\banner');
            //var_dump('../../'.APPPATH);
           // var_dump($image_path);
           // die();
            $config = array('upload_path'   => 'public/frontend/images/banner',
                'allowed_types' => 'gif|jpg|png',
                'max_size'      => '2000');

            
            $this->load->library("upload",$config);
            if(!$this->upload->do_upload("fileImage")){
                $arrayErrors= array($this->upload->display_errors());
                $data['msg']  =  "<div class='error'>".$arrayErrors[0]."</div>";
                $this->admin_layout->view("admin/addBanner",$data);
            }else{
                $image_data = $this->upload->data(); 
                $data = array(
                    'TenBanner' => $this->input->post('txtNameImage'),
                    'urlHinh'   => $image_data['file_name'],
                    'NgayDang'  => date('m/d/Y h:i:s a', time()),
                    'Active'    => $this->input->post('radioVisible'),
                    'SoLanClick' => '0',
                    'Mota' => $this->input->post('txtMota')
                );

                $this -> MBanners -> Insert($data);
                redirect('admin/banner/listBanner');
            }
        }
    }


    public function editBanner($id){

        $data['data']=$this->MBanners->GetBanner($id);
        $data['msg']='';
        $data['title'] = "Quản lý Banner";
       
        $data['navigation']['title'] = 'Quản lý Banner';
        $data['navigation']['active']= 'Sửa Banner';


        $config = array(
            array(
                 'field' => 'txtNameImage', 
                 'label' => 'Tên Ảnh', 
                 'rules' => 'required'
            ),
            array(
                 'field' => 'txtMota', 
                 'label' => 'Mô tả', 
                 'rules' => 'required'
            )
        );
        
        $this->form_validation->set_rules($config);
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $this->admin_layout->view("admin/editBanner",$data);
        } else {



         if (empty($_FILES['file_name']['name'])) {
                //echo "không file";
                $data = array(
                    'TenBanner' => $this->input->post('txtNameImage'),
                    'NgayDang'  => date('m/d/Y h:i:s a', time()),
                    'Active'    => $this->input->post('radioVisible'),
                    'Mota' => $this->input->post('txtMota')
                );

                $this -> MBanners -> Update($data, $id);
                redirect('admin/banner/listBanner');

            } else {
                $config = array('upload_path'   => 'public/frontend/images/banner',
                    'allowed_types' => 'gif|jpg|png',
                    'max_size'      => '2000');

                
                $this->load->library("upload",$config);
                if(!$this->upload->do_upload("fileImage")){
                    $arrayErrors= array($this->upload->display_errors());
                    $data['msg']  =  "<div class='error'>".$arrayErrors[0]."</div>";
                    $this->admin_layout->view("admin/editBanner",$data);
                }else{
                    $image_data = $this->upload->data(); 
                    $data = array(
                        'TenBanner' => $this->input->post('txtNameImage'),
                        'urlHinh'   => $image_data['file_name'],
                        'NgayDang'  => date('m/d/Y h:i:s a', time()),
                        'Active'    => $this->input->post('radioVisible'),
                        'Mota' => $this->input->post('txtMota')
                    );

                    $this -> MBanners -> Update($data, $id);
                    redirect('admin/banner/listBanner');
                }
            }
        }
    }
    public function deleteBanner() {
        if(isset($_REQUEST["Banner_id"])){
            $Banner_id = $_REQUEST["Banner_id"];
            $returnValue = $this ->MBanners->Delete($Banner_id);
            if($returnValue ==1) {
                echo "true";
            } else {
                echo "false";
            }
        }
    }
}
?>
